﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace OutputAllFilesToTXT
{
    internal class Program
    {
        static void Main(string[] args)
        {
            List<string> list = new List<string>();
            string path = System.AppDomain.CurrentDomain.BaseDirectory;
            Director(path, list);
            foreach (var key in all.Keys)
            {
                Console.WriteLine($"{key} ({all[key].Count})");
                foreach (var value in all[key])
                {
                    Console.WriteLine(value);
                }
                Console.WriteLine("-----------------------");
            }
            Console.ReadLine();
        }

        static Dictionary<string, List<string>> all = new Dictionary<string, List<string>>();
        public static void Director(string dir, List<string> list)
        {
            string[] files = Directory.GetFiles(dir, "*@#*");
            foreach (string f in files)
            {
                FileInfo fi = new FileInfo(f);
                list.Add(fi.FullName);//添加文件名到列表中并输出
                Console.WriteLine(fi.FullName);
                string simpleName = Path.GetFileNameWithoutExtension(fi.Name);
                List<string> tu = GetFileInfo(simpleName);
                
                foreach (var key in tu)
                {
                    if (!all.ContainsKey(key))
                    {
                        all.Add(key, new List<string>());
                    }
                    all[key].Add(fi.FullName);
                }
            }

            string[] directs = Directory.GetDirectories(dir);//文件夹
            //获取子文件夹内的文件列表，递归遍历  
            foreach (string dd in directs)
            {
                DirectoryInfo di = new DirectoryInfo(dd);
                Director(di.FullName, list);
            }
        }
        /// <summary>
        /// GetFileInfo
        /// </summary>
        /// <param name="fileName">KK@#Tag1#Tag2.exe</param>
        /// <returns><"KK", {"Tag1", "Tag2"}></returns>
        private static List<string> GetFileInfo(string simpleName) //Tuple<string, List<string>>
        {
            string realName = "";
            List<string> tags = new List<string>();
            string[] sArray = simpleName.Split(new char[] { '@' }, StringSplitOptions.RemoveEmptyEntries);
            if (sArray.Length > 1)
            {
                realName = sArray[0];
                string tagString = sArray[1];
                string[] tagArr = tagString.Split(new char[] { '#' }, StringSplitOptions.RemoveEmptyEntries);
                tags.AddRange(tagArr);
            }
            return tags;
        }

    }
}
